#include <bits/stdc++.h>
using namespace std;

template <typename T, template<typename> typename Cmp = greater>
class mono_deque : public deque<pair<int, T>> {
 public:
  mono_deque& push(int idx, const T &x) {
    while (!this->empty() && !Cmp<T>()(this->back().second, x)) this->pop_back();
    this->push_back({idx, x});
    return *this;
  }
  mono_deque& shrink_to(int idx) {
    while (!this->empty() && this->front().first <= idx) this->pop_front();
    return *this;
  }
  T get_extremum() { return this->front().second; }
};

int main() {
  int n, m, a;
  cin >> n >> m >> a;
  vector<vector<int>> maze(n, vector<int>(m));
  for (auto &row : maze) {
    for (auto &x : row)  cin >> x;
  }
  vector<vector<int>> row_max(n, vector<int>(m));
  vector<vector<int>> row_min(n, vector<int>(m));
  // [高度 a]
  for (int y = 0; y < m; y ++) {
    mono_deque<int, less> inc_q;
    mono_deque<int> dec_q;
    for (int x = 0; x < n; x ++) {
      row_min[x][y] = inc_q.push(x, maze[x][y]).shrink_to(x - a).get_extremum();
      row_max[x][y] = dec_q.push(x, maze[x][y]).shrink_to(x - a).get_extremum();
    }
  }
  int ans = INT_MAX;
  // [宽度 a]
  for (int x = a - 1; x < n; x++) {
    mono_deque<int, less> inc_q;
    mono_deque<int> dec_q;
    for (int y = 0; y < m; y++) {
      dec_q.push(y, row_max[x][y]).shrink_to(y - a);
      inc_q.push(y, row_min[x][y]).shrink_to(y - a);
      if (y >= a - 1) {
        ans = min(ans, dec_q.get_extremum() - inc_q.get_extremum());
      }
    }
  }
  cout << ans << endl;
}